﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Hr.IDAL;
using Hr.Model;

namespace Hr.DAL
{
    public partial class MailDAL : BaseDAL<Mail_List>, IMail_ListDAL
    {
        private DbContext db = DbContextFactory.Create();
        /// <summary>
        /// 添加内部邮件到收件箱
        /// </summary>
        /// <param name="m"></param>
        public void SendMail1(Mail_List m,string EmailContent,string CheckDt)
        {

            
            //查询接收人集合
            List<User_List> UL = db.Set<User_List>().Where(n => CheckDt.IndexOf(n.pk_User.ToString()) != -1).ToList();

            //添加的信件集合
            //List<Mail_List> ML = new List<Mail_List>();
            foreach (User_List item in UL)
            {
                Mail_List Ml = new Mail_List();
                Ml.fk_User = int.Parse(m.MailFrom);//发件人id
                Ml.fk_Department = item.pk_User;//收件人id
                Ml.MailFrom =db.Set<User_List>().SingleOrDefault(n=>n.pk_User.ToString()== m.MailFrom).FullName;//发件人姓名
                Ml.MailTo = db.Set<User_List>().SingleOrDefault(n => n.pk_User == item.pk_User).FullName;//收件人姓名
                Ml.Subject = m.Subject;//邮件标题
                Ml.Body = EmailContent;//邮件内容
                Ml.SentDate = DateTime.Now;//发送日期
                Ml.IsStar = true;//是否为内部邮件
                Ml.TypeID = m.TypeID;//类型
                Ml.StatusID = m.StatusID;//状态
                Ml.IsForward = false;//是否在回收站
                Ml.ParentID = 0;//收件箱

                //ML.Add(MM);
                db.Set<Mail_List>().Add(Ml);
            }
        }
        public void AddSendMail( string MailFrom, string Addressee,string MailboxTitle, string EmailContent)
        {



                Mail_List Ml = new Mail_List();

                Ml.MailFrom = MailFrom;//发件人姓名
                Ml.MailTo = Addressee;//收件人姓名
                Ml.Subject = MailboxTitle;//邮件标题
                Ml.Body = EmailContent;//邮件内容
                Ml.SentDate = DateTime.Now;//发送日期
                Ml.IsStar = false;//是否为内部邮件

                //ML.Add(MM);
                db.Set<Mail_List>().Add(Ml);
            
        }
        public IEnumerable<object> Mail1()
        {
            List<Mail_List> Mail = db.Set<Mail_List>().Where(n =>  n.IsStar == false).ToList();
            return (from MailList in Mail
                    select new
                    {
               
                        MailList.MailFrom,
                        MailList.MailTo,
                        MailList.Subject,
                        MailList.Body,
                        MailList.SentDate,
                        MailList.IsStar
                       
                    }
                    ).ToList();
        }
        /// <summary>
        /// 添加内部邮件到草稿箱
        /// </summary>
        /// <param name="m"></param>

        public void AddDraftBox(Mail_List m, string EmailContent, string CheckDt)
        {
            //添加的信件集合
            //List<Mail_List> ML = new List<Mail_List>();
                Mail_List Ml = new Mail_List();
                Ml.MailFrom = m.MailFrom;//发件人
                Ml.Subject = m.Subject;//邮件标题
                Ml.Body = EmailContent;//邮件内容
                Ml.SentDate = DateTime.Now;//发送日期
                Ml.IsStar = true;//是否为内部邮件
                Ml.TypeID = m.TypeID;//类型
                Ml.StatusID = m.StatusID;//状态
                Ml.IsForward = false;//是否在回收站
                Ml.ParentID = 2;//草稿箱
                Ml.MailTo = CheckDt;//收件人
                //ML.Add(MM);
                db.Set<Mail_List>().Add(Ml);
        }
        /// <summary>
        /// 将内部邮件放入回收站
        /// </summary>
        /// <param name="m"></param>
        /// <param name="EmailContent"></param>
        public void UbMail(string pk_Mail)
        {
            Mail_List Ml = db.Set<Mail_List>().SingleOrDefault(n => n.pk_Mail.ToString() == pk_Mail);
            Ml.IsForward = true;
        }
        /// <summary>
        /// 从回收站恢复一封邮件
        /// </summary>
        /// <param name="m"></param>
        /// <param name="EmailContent"></param>
        public void UbDelMail(string pk_Mail)
        {
            Mail_List Ml = db.Set<Mail_List>().SingleOrDefault(n => n.pk_Mail.ToString() == pk_Mail);
            Ml.IsForward = false;
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户里的邮件 可以判断在哪个文件夹
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=发件箱2=草稿箱</param>
        /// <returns></returns>
        public IEnumerable<object> GetMailInfo(string Pk_User,int ParentID)
        {
            //string list = "1|2|2";
            //list.IndexOf(id);
            //return db.Set<Resume_List>().ToList();
            List<Mail_List> Mail= db.Set<Mail_List>().Where(n => n.fk_Department.ToString()==Pk_User&&n.ParentID== ParentID&&n.IsStar==true && n.IsForward == false).ToList();
            return (from MailList in Mail    
            select new
                    {
                        TypeNmae_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.TypeID).FirstOrDefault().TypeName_CHS,
                        MailList.pk_Mail,
                        MailList.MailTo,
                        MailList.Subject,
                        MailList.Body,
                        MailList.SentDate,
                        MailList.ParentID,
                        MailList.MailFrom,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.StatusID).FirstOrDefault().StatusName_CHS,
                   
                    }
                    ).ToList();
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户  回收站中的邮件 
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=发件箱2=草稿箱</param>
        /// <returns></returns>
        public IEnumerable<object> GetDelMailInfo1(string Pk_User)
        {
            //string list = "1|2|2";
            //list.IndexOf(id);
            //return db.Set<Resume_List>().ToList();
            List<Mail_List> Mail = db.Set<Mail_List>().Where(n => n.fk_Department.ToString() == Pk_User  && n.IsStar == true && n.IsForward == true).ToList();
            return (from MailList in Mail
                    select new
                    {
                        TypeNmae_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.TypeID).FirstOrDefault().TypeName_CHS,
                        MailList.pk_Mail,
                        MailList.MailTo,
                        MailList.Subject,
                        MailList.Body,
                        MailList.SentDate,
                        MailList.ParentID,
                        MailList.MailFrom,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.StatusID).FirstOrDefault().StatusName_CHS,

                    }
                    ).ToList();
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户里的邮件 发件箱
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=发件箱2=草稿箱</param>
        /// <returns></returns>
        public IEnumerable<object> GetOutboxInfo(string MailFrom)
        {
            //string list = "1|2|2";
            //list.IndexOf(id);
            //return db.Set<Resume_List>().ToList();
            List<Mail_List> Mail = db.Set<Mail_List>().Where(n => n.fk_User.ToString()== MailFrom  && n.IsStar == true && n.IsForward == false).ToList();
            return (from MailList in Mail
                    select new
                    {
                        TypeNmae_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.TypeID).FirstOrDefault().TypeName_CHS,
                        MailList.pk_Mail,
                        MailList.MailTo,
                        MailList.Subject,
                        MailList.Body,
                        MailList.SentDate,
                        MailList.ParentID,
                        MailList.MailFrom,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.StatusID).FirstOrDefault().StatusName_CHS,
                    }
                    ).ToList();
        }
        /// <summary>
        /// 通过id查询邮件
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <returns></returns>
        public object GetDraftboxInfo(string pk_Mail)
        {
            //string list = "1|2|2";
            //list.IndexOf(id);
            //return db.Set<Resume_List>().ToList();
            List<Mail_List> Mail = db.Set<Mail_List>().Where(n => n.pk_Mail.ToString()== pk_Mail).ToList();
            return (from MailList in Mail
                    select new
                    {
                        TypeNmae_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.TypeID).FirstOrDefault().TypeName_CHS,
                        MailList.pk_Mail,
                        MailList.MailTo,
                        MailList.Subject,
                        MailList.Body,
                        MailList.SentDate,
                        MailList.ParentID,
                        MailList.MailFrom,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.StatusID).FirstOrDefault().StatusName_CHS,

                    }
                    ).FirstOrDefault();
        }
        /// <summary>
        /// 查询所有内部邮件
        /// </summary>
        /// <returns></returns>
        public Mail_List MailListInfo()
        {
            
            return db.Set<Mail_List>().SingleOrDefault(n => n.IsStar == true);
        }
        /// <summary>
        /// 通过邮件id删除指定邮件
        /// </summary>
        /// <param name="pk_Mail"></param>
        public void DelMail(string pk_Mail)
        {
            //查出要删除的邮件
            List<Mail_List> MenuList = db.Set<Mail_List>().Where(n => n.pk_Mail.ToString() == pk_Mail).ToList();
            //删除菜单集合
            db.Set<Mail_List>().RemoveRange(MenuList);
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户回收站里的邮件
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=发件箱2=草稿箱</param>
        /// <returns></returns>
        public IEnumerable<object> GetDelMailInfo(string Pk_User)
        {
            //string list = "1|2|2";
            //list.IndexOf(id);
            //return db.Set<Resume_List>().ToList();
            List<Mail_List> Mail = db.Set<Mail_List>().Where(n => n.MailTo.ToString().IndexOf(Pk_User) > -1  && n.IsStar == true && n.IsForward == true).ToList();
            return (from MailList in Mail
                    select new
                    {
                        TypeNmae_CHS = db.Set<Sys_TypeID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.TypeID).FirstOrDefault().TypeName_CHS,
                        MailList.pk_Mail,
                        MailList.MailTo,
                        MailList.Subject,
                        MailList.Body,
                        MailList.SentDate,
                        MailList.ParentID,
                        MailList.MailFrom,
                        StatusNmae_CHS = db.Set<Sys_StatusID>().Where(n => n.TableName.Equals("Mail_List") & n.ID == MailList.StatusID).FirstOrDefault().StatusName_CHS,

                    }
                    ).ToList();
        }

        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
